<template>
    <div class="home-container">
        <!-- 用户信息卡片 -->
        <div class="user-card">
            <div class="user-info-section">
                <div class="avatar-container">
                    <el-avatar :size="100" :icon="UserFilled" />
                </div>
                <div class="user-details">
                    <h2>{{ administratorName }}</h2>
                    <p>账号: {{ administratorAccount }}</p>
                </div>
                <el-button class="logout-btn" @click="handleLogout">退出登录</el-button>
            </div>
            
            <!-- 个人信息修改链接 -->
               <div class="profile-edit-link" @click="goToProfile">
               <span>个人信息</span>
                <el-icon><ArrowRight /></el-icon>
               </div>
               <div class="profile-edit-link" >
                <router-link to="/alter-password">
               <span>修改密码</span>
                <el-icon><ArrowRight /></el-icon>
                </router-link>
               </div>

        </div>
    </div>
</template>

<script>
import { UserFilled } from '@element-plus/icons-vue'
import { ArrowRight } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
// import { modifyHeadPic } from '@/api/manager'
// import { me } from '@/api/manager'

export default {
    name: 'Home',
    setup() {
        const router = useRouter()
        const administratorName = ref('')
        const administratorAccount = ref('')

        onMounted(() => {
            // 从 localStorage 获取管理员信息
            administratorName.value = localStorage.getItem('administrator_name') || '未知用户'
            administratorAccount.value = localStorage.getItem('administrator_account') || ''
        })

        const handleLogout = () => {
            try {
                // 清除 token
                localStorage.removeItem('token');
                // 清除用户信息
                localStorage.removeItem('administrator_name');
                localStorage.removeItem('administrator_account');
                
                // 提示用户
                ElMessage.success('退出登录成功');
                
                // 跳转到登录页面
                router.push('/login');
            } catch (error) {
                console.error('退出登录错误:', error);
                ElMessage.error('退出登录失败');
            }
        }

        const goToProfile = () => {
            router.push('/info')
        }
       

        return {
            UserFilled,
            ArrowRight,
            handleLogout,
            goToProfile,
            administratorName,
            administratorAccount
        }
    }
}
</script>

<style scoped>
.home-container {
    padding: 20px;
}

.user-card {
    background: #fff;
    border-radius: 8px;
    padding: 20px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.user-info-section {
    display: flex;
    align-items: center;
    padding-bottom: 20px;
    border-bottom: 1px solid #eee;
}

.avatar-container {
    margin-right: 20px;
}

.user-details {
    flex-grow: 1;
}

.user-details h2 {
    margin: 0;
    font-size: 18px;
    margin-bottom: 8px;
}

.user-details p {
    margin: 0;
    color: #666;
    font-size: 14px;
}

.logout-btn {
    background-color: #909399;
    color: white;
}

.profile-edit-link {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 0;
    cursor: pointer;
}

.profile-edit-link:hover {
    color: #409EFF;
}

.profile-edit-link span {
    font-size: 16px;
}
</style>